package com.xquant.marketdata.curvestrip.entity;

import com.xquant.marketdata.utils.ExcelAttribute;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.*;

/**
 * @Author: xpp -_- -_-
 * @Date: 2021/3/22 16:07
 * 违约率报价导入  评级模板实体类
 * 第2行2列开始
 */
public class TdailyQuoteGrade implements IDailyQuote {

    public final static int MODEL_TYPE = 2;

    @ExcelAttribute(name = "发布日期")
    private String begDate;
    @ExcelAttribute(name = "编码")
    private String iCode;//编码
    @ExcelAttribute(name = "评级")
    private String grade;//评级
    @ExcelAttribute(name = "期限")
    private String term;
    @ExcelAttribute(name = "价格类型")
    private String dpType;
    @ExcelAttribute(name = "价格(BP)")
    private String dpValue;
    //报卖价
    private BigDecimal dpAsk;
    //报买价
    private BigDecimal dpBid;
    //中间价
    private BigDecimal dpMid;

    private final static String DpType_dpAsk = "卖价";
    private final static String DpType_dpBid = "买价";
    private final static String DpType_dpMid = "均价";
    public final static List<String> titleList = Arrays.asList("序号","发布日期","编码","评级","期限","价格类型","价格(BP)");

    public String getiCode() {
        return iCode;
    }

    public void setiCode(String iCode) {
        this.iCode = iCode;
    }

    public String getTerm() {
        return term;
    }

    public void setTerm(String term) {
        this.term = term;
    }

    public String getDpType() {
        return dpType;
    }

    public void setDpType(String dpType) {
        this.dpType = dpType;
    }

    public String getBegDate() {
        return begDate;
    }

    public void setBegDate(String begDate) {
        this.begDate = begDate;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public String getDpValue() {
        return dpValue;
    }

    public void setDpValue(String dpValue) {
        this.dpValue = dpValue;
    }

    public BigDecimal getDpAsk() {
        return dpAsk;
    }

    public void setDpAsk(  BigDecimal dpAsk) {
        this.dpAsk = dpAsk;
    }

    public   BigDecimal getDpBid() {
        return dpBid;
    }

    public void setDpBid(  BigDecimal dpBid) {
        this.dpBid = dpBid;
    }

    public   BigDecimal getDpMid() {
        return dpMid;
    }

    public void setDpMid(  BigDecimal dpMid) {
        this.dpMid = dpMid;
    }

    public TdailyQuoteGrade() {
    }

    public TdailyQuoteGrade(String iCode, String term, String dpType, String dpValue, BigDecimal dpAsk, BigDecimal dpBid, BigDecimal dpMid) {
        this.iCode = iCode;
        this.term = term;
        this.dpType = dpType;
        this.dpValue = dpValue;
        this.dpAsk = dpAsk;
        this.dpBid = dpBid;
        this.dpMid = dpMid;
    }

    @Override
    public String toString() {
        return "TdailyQuoteGrade{" +
                "begDate='" + begDate + '\'' +
                ", iCode='" + iCode + '\'' +
                ", term='" + term + '\'' +
                ", dpType='" + dpType + '\'' +
                ", dpValue='" + dpValue + '\'' +
                ", dpAsk=" + dpAsk +
                ", dpBid=" + dpBid +
                ", dpMid=" + dpMid +
                '}';
    }

    public static List<TdailyQuoteGrade> resolveGrade(List<TdailyQuoteGrade> datas){
        Map<String, TdailyQuoteGrade> map = new WeakHashMap<>();
        for (TdailyQuoteGrade tdailyQuoteGrade : datas){
            String s = tdailyQuoteGrade.getiCode();
            String key = MessageFormat.format("{0}_{1}",s.substring(0, s.length() - 1),tdailyQuoteGrade.getBegDate());
            TdailyQuoteGrade grade = map.get(key);
            BigDecimal dpValue = new BigDecimal(tdailyQuoteGrade.getDpValue());
            BigDecimal bigDecimal = new BigDecimal("10000");
            BigDecimal divide = dpValue.divide(bigDecimal);
            if(grade == null) {
                grade = tdailyQuoteGrade;
                map.put(key, grade);
            }
            switch (tdailyQuoteGrade.getDpType()){
                case DpType_dpAsk:
                    grade.setDpAsk(divide);
                    map.put(key, grade);
                    continue;
                case DpType_dpBid:
                    grade.setDpBid(divide);
                    map.put(key, grade);
                    continue;
                default:
                    grade.setDpMid(divide);
                    map.put(key, grade);
                    continue;
            }
        }
        return new ArrayList<TdailyQuoteGrade>(map.values());
    }

}
